package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author jack
 * @data 2024 9:16
 */
@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 查询所有频道 -- 启用
     *
     * @return
     */
    @Override
    public ResponseResult findAll() {
        //查询所有启用频道
        List<WmChannel> list = list(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getStatus, true));
        return ResponseResult.okResult(list);
    }

    /**
     * 新增频道
     *
     * @param wmChannel WM通道
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult insert(WmChannel wmChannel) {
        // 判断前端传过来的参数是否为空
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 判断新增的频道名称是否存在
        List<WmChannel> list = list(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (list.size() > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已存在");
        }
        // 修改新增时间
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 按名称查询页面
     *
     * @param channelDto 通道 DTO
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult pageQueryByName(ChannelDto channelDto) {
        // 校验前端传的参数是否为空
        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 进行分页查询
        Page page = new Page(channelDto.getPage(), channelDto.getSize());
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        // 按照频道名称进行模糊查询
        // 判断前端传递过来的频道名称是否为空
        // 按照时间进行倒叙查询
        wrapper.like(channelDto.getName() != null, WmChannel::getName, channelDto.getName())
                .eq(channelDto.getStatus() != null, WmChannel::getStatus, channelDto.getStatus())
                .orderByDesc(WmChannel::getCreatedTime);
        // 对结果进行封装并返回
        page = page(page, wrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /**
     * 修改频道
     *
     * @param wmChannel WM通道
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult update(WmChannel wmChannel) {
        // 判断参数是否为空
        if (wmChannel == null || wmChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 定义一个整型变量count，用来存储引用该频道的新闻数量
        int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId())
                .eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode()));
        // 如果count大于0，则说明该频道被引用
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "该频道被引用，不能修改");
        }
        // 修改操作
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 按 ID 删除
     *
     * @param id 编号
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        // 先判断id是否为空
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 根据id查询当前频道
        WmChannel wmChannel = getById(id);
        if(wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 判断频道是否有效
        if(wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "该频道已启用，不能删除");
        }
        // 判断频道是否为启用状态
        int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId())
                .eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode()));
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "该频道为启用状态，不能删除");
        }
        // 删除频道
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
